#!/usr/bin/env python

# -*- coding: utf-8 -*-

import json

import redis

import pymongo
import os,urllib

def main():

    # r = redis.Redis()

    r = redis.Redis(host='127.0.0.1',port=6379,db=0)

    client = pymongo.MongoClient(host='127.0.0.1', port=27017)

    db = client['dmoz']

    sheet = db['sheet']

    while True:
        print('-----------------------')
        # process queue as FIFO, change `blpop` to `brpop` to process as LIFO

        source, data = r.blpop(["meijutt:items"])

        item = json.loads(str(data, encoding='utf-8'))
        print(item)
        sheet.insert(item)

        try:

            print(u"Processing: %(name)s <%(link)s>" % item)

        except KeyError:

            print(u"Error procesing: %r" % item)
def test_yield():
    yield 2
    yield 3
    yield 4
    yield 5
    yield 6
    yield 7

def save_img(img_url,file_name,file_path='D:\meizi2'):
    #保存图片到磁盘文件夹 file_path中，默认为当前脚本运行目录下的 book\img文件夹
    try:
        if not os.path.exists(file_path):
            print ('文件夹',file_path,'不存在，重新建立')
            #os.mkdir(file_path)
            os.makedirs(file_path)
        #获得图片后缀
        file_suffix = os.path.splitext(img_url)[1]
        #拼接图片名（包含路径）
        filename = '{}{}{}{}'.format(file_path,os.sep,file_name,file_suffix)
       #下载图片，并保存到文件夹中
        urllib.request.urlretrieve(img_url,filename=filename)
    except IOError as e:
        print ('文件操作失败',e)
    except Exception as e:
        print ('错误 ：',e)
if __name__ == '__main__':
    img_url = 'https://www.mntup.com/uploadfile/201902/1/FE223342829.jpg'
    save_img(img_url, 'jianshu')